<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class GroupController extends AdminModuleController 
{
	public function actionAdmin() {
		$page = max(1, getgpc('page'));
		$sqladd = '';
		
		$userGroupList = $this->UserGroupModel->findAll($sqladd, $page, GODHOUSE_PPP3, "groupId ASC");
		$num = $this->UserGroupModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, 'group-admin.htm');
		
		$groupId = getgpc(2);
		$userGroup = $this->UserGroupModel->findByPk($groupId);
		
		if($groupId) {
			$submitTitle = '修改';
			$action = 'update';
		} else {
			$submitTitle = '创建';
			$action = 'create';
		}
		$this->view->assign('action', $action);
		$this->view->assign('submitTitle', $submitTitle);
		
		$this->view->assign('userGroup', $userGroup);
		$this->view->assign('userGroupList', $userGroupList);
		$this->view->assign('multipage', $multipage);
		$this->display('group_admin');
	}
	
	public function actionCreate() {
		if($_POST['userGroup']) {
			$userGroup = $_POST['userGroup'];
			if(!$userGroup['groupName']) {
				$this->alert('请填写用户组名称', 'BACK');
			}
			/*
			if (!$userGroup['discount'] || !is_numeric($userGroup['discount'])) {
				$this->alert('请填写正确的折扣比例', 'BACK');
			}*/
			$this->UserGroupModel->insert($userGroup);
			$this->redirect('group-admin.htm');
		}
		$this->display('group_admin');
	}
	
	public function actionUpdate() {
	
		$uArr = $this->UserModel->fetchAll("groupId = '1'");
				
		if($_POST['userGroup']) {
			$userGroup = $_POST['userGroup'];
			$groupId = (int)$userGroup['groupId'];
			
			if(!$userGroup['groupName']) {
				$this->alert('请填写用户组名称', 'BACK');
			}
			
			$this->UserGroupModel->updateByPk($groupId, $userGroup);
			
			//修改购物车中还没有支付的商品的折扣
			/*
			$userIdStr = '';
			$uArr = $this->UserModel->fetchAll("groupId = '$groupId'");
			foreach($uArr as $v) {
				$userIdStr .= $v['userId'].',';
			}
			$this->ShoppingcartModel->update("settleStatus='0'", array('discount'=>$userGroup['discount']/100));		
			*/
			
			$this->UserModel->fetchAll("groupId = '$groupId'");			
			$this->redirect('group-admin.htm');
		}
		$this->display('group_admin');
	}
	
	public function actionDelete() {
		$groupId = getgpc(2); 
		$this->UserGroupModel->deleteByPk($groupId);
		$this->redirect('group-admin.htm');
	}
}